package com.example.studentmanager.service.impl;

import com.example.studentmanager.mapper.CourseMapper;
import com.example.studentmanager.mapper.StuCourseMapper;
import com.example.studentmanager.mapper.StudentMapper;
import com.example.studentmanager.model.Course;
import com.example.studentmanager.model.vo.CourseVo;
import com.example.studentmanager.model.vo.QueryVo;
import com.example.studentmanager.model.StuCourse;
import com.example.studentmanager.model.Student;
import com.example.studentmanager.service.StudentService;
import com.example.studentmanager.utils.BaseDao;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;



@Service
public class StudentServiceImpl extends Thread implements StudentService {



    @Autowired
    Student student;

    @Autowired
    List<Student> students;





   List<Course> courseList;

    @Autowired
    Course course;



    SqlSession sqlSession= new BaseDao().getSession();
    //AdministratorMapper administratorMapper=sqlSession.getMapper(AdministratorMapper.class);
    StudentMapper studentMapper=sqlSession.getMapper(StudentMapper.class);




   public StudentServiceImpl(){}




   // private Course course;



    @Override
    public String addStu(Student student) {
        int a;
        String result;
      StudentMapper studentMapper=sqlSession.getMapper(StudentMapper.class);
        try {
            a=studentMapper.addStudent(student);
            if(a>0){
                result="success";
            }else{
                result="fail";
            }
            return result;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Student searchStudentByStunum(int stunum) {

        StudentMapper studentMapper=sqlSession.getMapper(StudentMapper.class);
        try {
             student=studentMapper.selectStudentByStunum(stunum);
            return student;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @lombok.SneakyThrows
    @Override
    @Cacheable(value = "stuV",key = "'stuKey'")
    public List<Student> searchStudentByStuname(String stuname) {
        StudentMapper studentMapper=sqlSession.getMapper(StudentMapper.class);
            students=studentMapper.selectStudentByStuname(stuname);
            return students;
    }

    @Override
    public List<Student> searchByids(QueryVo queryVo) {
        List<Student> students=new ArrayList<>();
        StudentMapper studentMapper=sqlSession.getMapper(StudentMapper.class);
        students=studentMapper.selectByids(queryVo);
        return students;
    }

    @Override
    public List<Student> searchStuCourse() {
        students=new ArrayList<>();

        students=studentMapper.selcetStuCourse();
        return students;
    }

    @Override
    public void delStuById(Integer stunum) {

        try {
           int i=studentMapper.deleteStuById(stunum);
            System.out.println("删除结果："+i);
            sqlSession.commit();
        } catch (SQLException e) {
            e.printStackTrace();
        }


    }

    @Override
    public void updateStu(Student s) {
        studentMapper=sqlSession.getMapper(StudentMapper.class);
        try {
            studentMapper.updateStu(s);
            sqlSession.commit();
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }


    @Override
    public List<Student> searchAll(Integer page, Integer rows, String searchCol, String searchValue) {
        studentMapper=sqlSession.getMapper(StudentMapper.class);

        Integer start=(page-1)*rows;
        List<Student> students= null;
        try {
            students = studentMapper.searchAll(start,rows,searchCol,searchValue);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if(students!=null)
            return students;
        else
            throw new RuntimeException("未查询到");
    }


    @Override
    public Integer counts(String searchCol, String searchValue) {
        return studentMapper.counts(searchCol, searchValue);
    }


    @Override
    public void chooseCourse(Course course) {
        run();
    }




    @Override
    public List<Student> blurSelectStudent(Student student) {

        List<Student> students=studentMapper.blurSelectStudent(student);
        return students;
    }
/*

    public void run() {
        //chooseCourse.getCourse(course);
        //int a=course.getCredithour();
        //chooseCourse=new ChooseCourseImpl();
        //chooseCourse.getCourse(course);
        //CourseMapper courseMapper=sqlSession.getMapper(CourseMapper.class);

        //courseMapper.deleteCourse(this.course);
        int size = this.chooseCourse.getCourse().size() - 1;
        if(size>0) {
            StuCourse stuCourse = new StuCourse();
            System.out.println("消费者" + (this.chooseCourse.getCourse().size() - 1));
            Integer stu = 31707042;
            CourseMapper courseMapper = sqlSession.getMapper(CourseMapper.class);
            StuCourseMapper stuCourseMapper = sqlSession.getMapper(StuCourseMapper.class);
            System.out.println(this.chooseCourse.getCourse().get(size).getCourseid());
            System.out.println(stuCourse.toString());
            stuCourse.setCid(this.chooseCourse.getCourse().get(0).getCourseid());
            stuCourse.setSid(stu);
            stuCourseMapper.insertSc(stuCourse);
            sqlSession.commit();
            try {
                courseMapper.deleteCourse(this.chooseCourse.getCourse().get(size));
                sqlSession.commit();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        --size;
        //course.setCredithour(--a);
    }
*/



}
